<script lang="ts" setup name="XtxCity">
import { AddrssType, AreaList } from './city'
import { ref, watch, watchEffect } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
// 父传子数据
defineProps<{
  Address?: string
}>()
// 子传父
const emit = defineEmits<{
  (e: 'changeAddress', val: AddrssType): void
}>()
// 1.---- 控制省市区选项区的显示与隐藏的布尔值
const active = ref(false)
// 切换显示城市选择方法
const toggle = () => {
  active.value = !active.value
}
// 2.------ 点击城市选择关闭 弹层的dom元素绑定
const target = ref(null)
// 点击城市区域之外关闭弹层的方法
onClickOutside(target, () => {
  //  将active设置为false
  active.value = false
})
// 3-- 省市区的基本渲染


// 3.2省市区的存储数据
const areaList = ref<AreaList[]>([])
// 备份的省市区的数据
const catchList = ref<AreaList[]>([])
// 3.3--- 获取省市区的方法
const getAreaList = async () => {
  const res = await axios.get<AreaList[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  areaList.value = res.data
  catchList.value = res.data
}
getAreaList()
// 4.----选择业务
// 4.1 选择省市区数据存储
const changeResult = ref<AddrssType>({
  // 省份信息
  provinceCode: '',
  provinceName: '',
  // 市信息
  cityCode: '',
  cityName: '',
  // 区/县信息
  countyCode: '',
  countyName: ''
})
const selectArea = (item: AreaList) => {
  // console.log(item);
  // 在每点击一次 记录点击的area  不仅要记录名字 还有记录对应的code
  if (item.level === 0) {
    changeResult.value.provinceCode = item.code
    changeResult.value.provinceName = item.name
    areaList.value = item.areaList
  }
  if (item.level === 1) {
    changeResult.value.cityCode = item.code
    changeResult.value.cityName = item.name
    areaList.value = item.areaList
  }
  if (item.level === 2) {
    changeResult.value.countyCode = item.code
    changeResult.value.countyName = item.name
    // 省市区选择完毕后关闭弹层即可
    active.value = false
    // 子传父,将选择的地址信息传递给父组件
    // this.$emit('事件名', 参数)
    emit('changeAddress', changeResult.value)
  }
}
// 4.2 监听active的变化 为false 将数据重置一份
watch(active, () => {
  if (!active.value) {
    areaList.value = catchList.value
  }
})
/* watchEffect(() => {
  if (!active.value) {
    areaList.value = catchList.value
  }
}) */
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active: active }" @click="toggle">
      <span class="value" v-if="Address">{{ Address }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span class="ellipsis" v-for="item in areaList" :key="item.code" @click="selectArea(item)">{{ item.name }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    >span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>